/*
 *  copy source from lua-5.1.4 to learn lua implementation
 */
#ifndef lzio_h
#define lzio_h

#include "lua.h"

#include "lmem.h"

typedef struct Zio ZIO;

typedef struct Mbuffer {
  char *buffer;
  size_t n;
  size_t buffsize;
} Mbuffer;

#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)

struct Zio {
  size_t n;     /* bytes still unread */
  const char *p;    /* current position in buffer */
  lua_Reader reader;
  void* data;     /* additional data */
  lua_State *L;     /* Lua state (for reader) */
};

#define luaZ_resizebuffer(L, buff, size) \
    (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
      (buff)->buffsize = size)

#define luaZ_freebuffer(L, buff)  luaZ_resizebuffer(L, buff, 0)

#endif
